n = int(input())
brick = []
for _ in range(n):
    brick.append(tuple(map(int, input().split())))
brick.sort(key=lambda x: x[0] + x[1])
dp = [{brick[ii][0]: brick[ii][1]} for ii in range(n)]
max_value = 0
for i in range(n):
    for j in range(i):
        for weight, value in dp[j].items():
            if brick[i][1] >= weight:
                dp[i][weight + brick[i][0]] = max(value + brick[i][1], dp[i].get(weight + brick[i][0], 0))
                max_value = max(max_value, dp[i][weight + brick[i][0]])
print(max_value)
